/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.android.music.util;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

import com.android.music.R;
import com.android.music.view.ViewDeskTopLrc;

public class DeskLrcoperate implements OnTouchListener, OnClickListener {

    private Context mContext;

    private float mLastMotionY;

    private SharedPreference mSharePreference;

    public DeskLrcoperate(Context context) {
        mContext = context;
        mSharePreference = new SharedPreference(mContext);
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mDisplay = inflater.inflate(R.layout.display, null);
        mDisplay.setOnTouchListener(this);
        teskTopLrcView = (ViewDeskTopLrc) mDisplay.findViewById(R.id.play);
        imageView = (ImageView) mDisplay.findViewById(R.id.menu_control);
        imageView.setOnClickListener(this);
        params = new WindowManager.LayoutParams();
        // desklrcSetting = mDisplay.findViewById(R.id.desklrc_setting);
        // desklrcSetting.setVisibility(View.GONE);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        wm = (WindowManager) mDisplay.getContext().getSystemService("window");
        // mDisplay.setOnClickListener(this);
    }

    private View mDisplay;

    private ViewDeskTopLrc teskTopLrcView;

    private ImageView imageView;

    private WindowManager.LayoutParams params;

    private WindowManager wm;

    public void show() {
        if (!mSharePreference.getBoolean(Util.IS_DESK_TOP_LRC_SHOW))
            wm.addView(mDisplay, params);
    }

    public void dismiss() {
        WindowManager wm = (WindowManager) mDisplay.getContext().getSystemService("window");
        if (mDisplay.getParent() != null) {
            if (!mSharePreference.getBoolean(Util.IS_DESK_TOP_LRC_SHOW))
                wm.removeView(mDisplay);
        }
    }

    public void changeMessage(int position) {
        teskTopLrcView.setlrcPosition(position);
    }

    public void initLrcMap() {
        teskTopLrcView.initLrcMap();
    }

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            case MotionEvent.ACTION_DOWN:
                mLastMotionY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final int yDiff = (int) (event.getY() - mLastMotionY);
                params.y = params.y + yDiff;
                wm.updateViewLayout(mDisplay, params);
                break;
        }
        return false;
    }

    public void onClick(View v) {
        if (v == imageView) {
            dismiss();
            // desklrcSetting.setVisibility(View.VISIBLE);
        }

    }

}
